﻿using System;
using System.IO;

namespace _091.TestOrder
{
	internal class Program
	{
		static void Main()
		{
			//string folderPath = @"C:\Users\zhineng.fan\Desktop\vbf";

			//DeleteUnwantedFiles(folderPath, 0);

			//Console.WriteLine("删除完成！");
			//ExtractVbfFiles(folderPath, @"C:\Users\zhineng.fan\Desktop\vbf\VbfFile");

			//Console.WriteLine("提取完成！");
			//Console.ReadLine();
			TestChangeName();
		}


		public static void TestChangeName()
		{
			string path = "C:\\Users\\zhineng.fan\\Desktop\\vbf\\VbfFile";
			string[] vbfs = Directory.GetFiles(path, "*.vbf");

			foreach (string vbf in vbfs)
			{
				string name = Path.GetFileNameWithoutExtension(vbf);
				if (name.Length == 11)
				{
					string newname = name.Substring(0, 10) + ".vbf";
					File.Move(vbf, Path.Combine(path, newname));
				}
			}
		}

		/// <summary>
		/// 提取vbf文件到文件夹中
		/// </summary>
		/// <param name="sourceFolderPath"></param>
		/// <param name="destinationFolderPath"></param>
		static void ExtractVbfFiles(string sourceFolderPath, string destinationFolderPath)
		{
			if (!Directory.Exists(destinationFolderPath))
			{
				Directory.CreateDirectory(destinationFolderPath);
			}

			string[] vbfFiles = Directory.GetFiles(sourceFolderPath, "*.vbf", SearchOption.AllDirectories);

			foreach (string vbfFile in vbfFiles)
			{
				string destinationFilePath = Path.Combine(destinationFolderPath, Path.GetFileName(vbfFile));

				File.Copy(vbfFile, destinationFilePath, true);
			}
		}

		/// <summary>
		/// 删除不是.vbf文件、.xlsx文件 和子文件夹大于三层
		/// </summary>
		/// <param name="folderPath"></param>
		/// <param name="currentLevel"></param>
		static void DeleteUnwantedFiles(string folderPath, int currentLevel)
		{
			string[] files = Directory.GetFiles(folderPath);
			string[] directories = Directory.GetDirectories(folderPath);

			foreach (string file in files)
			{
				string extension = Path.GetExtension(file);

				// 删除不以.vbf或.xlsx结尾的文件
				// 并且 .vbf文件大于10M 也想要删除
				if (extension == ".vbf")
				{
					// 获取文件大小（以字节为单位）
					long fileSizeInBytes = new FileInfo(file).Length;
					// 转换为MB
					double fileSizeInMB = fileSizeInBytes / (1024.0 * 1024.0);

					// 删除大于10MB的.vbf文件
					if (fileSizeInMB > 10)
					{
						File.Delete(file);
					}
				}
				else if (extension != ".xlsx")
				{
					File.Delete(file);
				}
			}
			// 只处理前两层文件夹
			if (currentLevel <= 2)
			{
				foreach (string directory in directories)
				{
					DeleteUnwantedFiles(directory, currentLevel + 1);
				}
			}
			else
			{
				// 删除第三层及其子文件夹
				Directory.Delete(folderPath, true);
			}
		}
	}
}
